/**
 *
 */
package com.angel.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author William
 *
 */
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public abstract class GenericTransactionalSpringTestCase extends GenericSpringTestCase {

	@Autowired
	SessionFactory sessionFactory = null;
	private boolean executeStatements = false;

	protected Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	/*TODO Change comments.
	 * Este metodo se llama antes de comenzar una transaccion.
	 * Debido a que solo los test que corran bajo transaccion deben
	 * ejecutar el flush de la session, se introduce esta variable
	 * de desicion.
	 * ver afterTestTransaccionl, flushSession
	 */
	@BeforeTransaction
	public void beforeTestTransaccional() {
		this.setExecuteStatements(true);
	}

	/*TODO Change comments.
	 * Este metodo se llama despuede de finalizar una transaccion (por
	 * commit o rollback).
	 * Debido a que solo los test que corran bajo transaccion deben
	 * ejecutar el flush de la session, se introduce esta variable
	 * de desicion.
	 * ver beforeTestTransaccional, flushSession
	 */
	@AfterTransaction
	public void afterTestTransaccionl() {
		this.setExecuteStatements(false);
	}

	/**
	 * @return the executeStatements
	 */
	protected boolean isExecuteStatements() {
		return executeStatements;
	}

	/**
	 * @param executeStatements the executeStatements to set
	 */
	protected void setExecuteStatements(boolean executeStatements) {
		this.executeStatements = executeStatements;
	}
}
